package com.aruba.mvvmapplication.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import com.aruba.mvvmapplication.databinding.ItemBinding
import com.aruba.mvvmapplication.model.MovieItemModel

/**
 * Created by aruba on 2021/9/22.
 */
class MoviePagingAdapter : PagingDataAdapter<MovieItemModel, BindingViewHolder>(
    object : DiffUtil.ItemCallback<MovieItemModel>() {
        override fun areItemsTheSame(oldItem: MovieItemModel, newItem: MovieItemModel): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: MovieItemModel, newItem: MovieItemModel): Boolean {
            return oldItem == newItem
        }
    }
) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingViewHolder {
        val binding = ItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return BindingViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BindingViewHolder, position: Int) {
        if (getItem(position) != null)
            (holder.binding as ItemBinding).movie = getItem(position)
    }

}